package com.xianggu.agency.demo.impl;

import com.xianggu.agency.demo.MyHandler;
import com.xianggu.agency.demo.MyInterface;

import java.lang.reflect.Field;

public class MyInterfaceImpl2 implements MyHandler {

        MyInterface myInterface;

        public MyInterfaceImpl2(MyInterface myInterface) {
            this.myInterface = myInterface;
        }

        @Override
        public String function(String method, String param) {
            StringBuilder sb = new StringBuilder();
            sb.append("System.out.println(\"前置增强\");");
            sb.append("myInterface." + method + "();\n");
            sb.append("System.out.println(\"后置增强\");");

            return sb.toString();
        }

        @Override
        public void setPorxy(Object proxy) {
            Class<?> aClass = proxy.getClass();
            Field field = null;
            try {
                field = aClass.getDeclaredField("myInterface");
                field.setAccessible(true);
                field.set(proxy , myInterface);
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }